package cn.com.dashihui.pay.SQ.Kit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;


public class MD5Utils {

	public static String getMd5InfoStr(Map<String, String> signMap) throws Exception {
		Collection<String> keyset = signMap.keySet();
		List<String> list = new ArrayList<String>(keyset);
		Collections.sort(list);

		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			String keyStr = (String) list.get(i);
			String value = ParamUtils.toString(signMap.get(keyStr));
			if ((null != value) && (!"".equals(value))) {
				buffer.append(keyStr + "=" + value + "&");
			} else if ("authMsg".equals(keyStr)) {
				buffer.append(keyStr + "=" + value + "&");
			}
		}

		return buffer.toString();
	}

	public static String getSign(String signTxt, String md5Key) throws Exception {
		MD5 md5 = new MD5();
		String sign = signTxt + (md5.getMD5ofStr(md5Key).toUpperCase());
		return md5.getMD5ofStr(sign).toUpperCase();
	}
	
	
	public static String getMd5Str(Map<String, String> signMap) throws Exception {
		Collection<String> keyset = signMap.keySet();
		List<String> list = new ArrayList<String>(keyset);
		Collections.sort(list);

		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			String keyStr = (String) list.get(i);
			String value = ParamUtils.toString(signMap.get(keyStr));
			if ((null != value) && (!"".equals(value))) {
				buffer.append(keyStr + "=" + value + "&");
			} else{
				buffer.append(keyStr + "=" + value + "&");
			}
		}

		return buffer.toString();
	}
	
	
	public static void main(String[] args) throws Exception {
		String s = "authMsg=&bankCode=BOC&cardNo=6217908000001351482&cardType=1&custName=张晴&custType=01&idNo=452402199006121251&idType=0&merNo=168885&payType=XYPAY&phoneNo=13526765380&reqMsgId=20190512144025665265&25D55AD283AA400AF464C76D713C07AD";
	
		 MD5 md5 = new MD5();
	      String MD5key = "12345678";
	      
	      System.out.println((md5.getMD5Info(MD5key)).toUpperCase());
	      
		String	MD5Info = md5.getMD5Info(s).toUpperCase();
			
			System.out.println(MD5Info);
	}
}
